package com.cnap.service.impl;

import com.cnap.CnapApplicationTests;
import com.cnap.db.entity.ClusterInfoPO;
import com.cnap.db.entity.MicroServicePO;
import com.cnap.db.mapper.ClusterInfoMapper;
import com.cnap.model.schedule.engine.EngineClusterInfo;
import com.cnap.service.ClusterInfoService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.util.Collections;
import java.util.List;

/**
 * <类文件描述>
 *
 * @author xc
 **/
class ClusterInfoServiceImplTest extends CnapApplicationTests {

    @Autowired
    private ClusterInfoService clusterInfoService;

    @MockBean
    private ClusterInfoMapper clusterInfoMapper;

    @Test
    void saveOrUpdateClusterInfoWithNew() {
        Mockito.when(clusterInfoMapper.selectClusterInfoBySvcId("svcId")).thenReturn(null);
        MicroServicePO microServicePO = new MicroServicePO();
        microServicePO.setId("svcId");
        microServicePO.setApplicationId("appId");
        microServicePO.setNamespace("ns");

        Mockito.when(clusterInfoMapper.insertClusterInfo(Mockito.any(ClusterInfoPO.class))).thenReturn(1);

        Assertions.assertDoesNotThrow(()->{
            clusterInfoService.saveOrUpdateClusterInfo(microServicePO, Collections.singletonList(new EngineClusterInfo()));
        });
    }

    @Test
    void saveOrUpdateClusterInfoWithUpdate() {
        ClusterInfoPO clusterInfoPO = new ClusterInfoPO();
        Mockito.when(clusterInfoMapper.selectClusterInfoBySvcId("svcId")).thenReturn(clusterInfoPO);
        MicroServicePO microServicePO = new MicroServicePO();
        microServicePO.setId("svcId");
        microServicePO.setApplicationId("appId");
        microServicePO.setNamespace("ns");

        Mockito.when(clusterInfoMapper.updateClusterInfo(Mockito.any(ClusterInfoPO.class))).thenReturn(1);

        Assertions.assertDoesNotThrow(()->{
            clusterInfoService.saveOrUpdateClusterInfo(microServicePO, Collections.singletonList(new EngineClusterInfo()));
        });
    }

    @Test
    void deleteClusterInfoBySvcId() {
        Mockito.when(clusterInfoMapper.deleteClusterInfoBySvcId("svcId")).thenReturn(1);

        Assertions.assertDoesNotThrow(()->{
            clusterInfoService.deleteClusterInfoBySvcId("svcId");
        });
    }

    @Test
    void getClusterInfosByAppId() {
        Mockito.when(clusterInfoMapper.selectClusterInfoByAppId("appId")).thenReturn(Collections.singletonList(new ClusterInfoPO()));
        List<ClusterInfoPO> pos = clusterInfoService.getClusterInfosByAppId("appId");

        Assertions.assertEquals(1, pos.size());
    }

    @Test
    void getClusterInfoBySvcId() {
        Mockito.when(clusterInfoMapper.selectClusterInfoBySvcId("svcId")).thenReturn(new ClusterInfoPO());

        ClusterInfoPO po = clusterInfoService.getClusterInfoBySvcId("svcId");

        Assertions.assertNotNull(po);
    }

    @Test
    void saveOrUpdateClusterInfoWithNew1() {
        Mockito.when(clusterInfoMapper.selectClusterInfoBySvcId("svcId")).thenReturn(null);
        MicroServicePO microServicePO = new MicroServicePO();
        microServicePO.setId("svcId");
        microServicePO.setApplicationId("appId");
        microServicePO.setNamespace("ns");

        Mockito.when(clusterInfoMapper.insertClusterInfo(Mockito.any(ClusterInfoPO.class))).thenReturn(1);

        Assertions.assertDoesNotThrow(()->{
            clusterInfoService.saveOrUpdateClusterInfo(microServicePO, new ClusterInfoPO());
        });
    }
    @Test
    void saveOrUpdateClusterInfoWithUpdate1() {
        Mockito.when(clusterInfoMapper.selectClusterInfoBySvcId("svcId")).thenReturn(new ClusterInfoPO());
        MicroServicePO microServicePO = new MicroServicePO();
        microServicePO.setId("svcId");
        microServicePO.setApplicationId("appId");
        microServicePO.setNamespace("ns");

        Mockito.when(clusterInfoMapper.updateClusterInfo(Mockito.any(ClusterInfoPO.class))).thenReturn(1);


        Assertions.assertDoesNotThrow(()->{
            clusterInfoService.saveOrUpdateClusterInfo(microServicePO, new ClusterInfoPO());
        });
    }

    @Test
    void updateAlgInfo() {
        Mockito.when(clusterInfoMapper.selectClusterInfoBySvcId("svcId")).thenReturn(new ClusterInfoPO());
        Mockito.when(clusterInfoMapper.updateClusterInfo(Mockito.any())).thenReturn(1);

        Assertions.assertDoesNotThrow(()->{
            clusterInfoService.updateAlgDeployInfos("svcId", Collections.emptyList());
        });
    }
    @Test
    void updateAlgInfoWithNull() {
        Mockito.when(clusterInfoMapper.selectClusterInfoBySvcId("svcId")).thenReturn(null);

        Assertions.assertDoesNotThrow(()->{
            clusterInfoService.updateAlgDeployInfos("svcId", Collections.emptyList());
        });
    }

}